/*****************************************************************************
 * 
 * Copyright [2013] [Mervin.Wong]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 *****************************************************************************/
package base.feature;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import util.MathTool;

import base.Network;

/**
 *@name Degree.java
 *@description 
 *@author 王进法<Mervin.Wong>
 *@version 
 *@Date 2013-1-13上午11:13:31
 */
/*********************************************************************************
 *
 * 关于网络的度的计算：
 *
 **********************************************************************************/

public class Degree implements InterfaceDegree{
	private Network net = null;
	
	public Degree(){
		
	}
	public Degree(Network net){
		this.net = net;
	}
	
	public void set(Network net){
		this.net = net;
	}
	
	public Network get(){
		return this.net;
	}
	/*********************************************************************************
	 *
	 * 基本功能
	 *
	 **********************************************************************************/
	/**
	 * @name nodeDegree
	 * @description 单个节点的度
	 * @param nodeId
	 * @return int degree
	 */
	public int nodeDegree(Number nodeId){
		return this.net.getAdjNodes(nodeId).size();
	}
	
	/**
	 * @name nodeDegree
	 * @description 单个节点的度
	 * @param Network net
	 * @param nodeId
	 * @return int degree
	 */
	public int nodeDegree(Network net, Number nodeId){
		return net.getAdjNodes(nodeId).size();
	}
	
	/**
	 * @name nodesDegree
	 * @description 获取多个节点的度
	 * @param nodeIdSet
	 * @return HashMap<Number,Number> degrees
	 */
	public  HashMap<Number,Number> nodesDegree(Set<Number> nodeIdSet){
		HashMap<Number,Number> degreeMap = new HashMap<Number,Number>();
		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			Number nodeId = (Number) iterator.next();
			degreeMap.put(nodeId, this.nodeDegree(nodeId));
			
		}
		return degreeMap;
	}
	
	/**
	 * @name nodesDegree
	 * @description 获取多个节点的度
	 * @param Network net
	 * @param nodeIdSet
	 * @return HashMap<Number,Number> degrees
	 */
	public HashMap<Number,Number> nodesDegree(Network net, Set<Number> nodeIdSet){
		HashMap<Number,Number> degreeMap = new HashMap<Number, Number>();
		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			Number nodeId = (Number) iterator.next();
			degreeMap.put(nodeId, this.nodeDegree(net,nodeId));
			
		}
		return degreeMap;
	}
	
	/**
	 * @name nodeInDegree
	 * @description 节点的入度
	 * @param nodeId
	 * @return int inDegree
	 */
	public int nodeInDegree(Number nodeId){
		return this.net.getInDegreeNodesId(nodeId).size();
	}
	
	/**
	 * @name nodeInDegree
	 * @description 节点的入度
	 * @param Network net
	 * @param nodeId
	 * @return int inDegree
	 */
	public int nodeInDegree(Network net, Number nodeId){
		return net.getInDegreeNodesId(nodeId).size();
	}
	
	/**
	 * @name nodesInDegree
	 * @description 多个节点的入度
	 * @param nodeIdSet
	 * @return HashMap<Number,Number> inDegrees
	 */
	public HashMap<Number,Number> nodesInDegree(HashSet<Number> nodeIdSet){
		HashMap<Number,Number> degreeMap = new HashMap<Number,Number>();
		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			Number nodeId = (Number) iterator.next();
			degreeMap.put(nodeId, this.nodeInDegree(nodeId));
			
		}
		return degreeMap;
	}
	
	/**
	 * @name nodesInDegree
	 * @description 多个节点的入度
	 * @param Network net
	 * @param nodeIdSet
	 * @return HashMap<Number,Number> inDegrees
	 */
	public HashMap<Number,Number> nodesInDegree(Network net, HashSet<Number> nodeIdSet){
		HashMap<Number,Number> degreeMap = new HashMap<Number,Number>();
		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			Number nodeId = (Number) iterator.next();
			degreeMap.put(nodeId, this.nodeInDegree(net,nodeId));
		}
		return degreeMap;
	} 
	
	/**
	 * 
	 * @name nodeOutDegree
	 * @description 单个节点的出度
	 * @param nodeId
	 * @return int outDegree
	 */
	public int nodeOutDegree(Number nodeId){
		return this.net.getOutDegreeNodesId(nodeId).size();
	}
	
	/**
	 * 
	 * @name nodeOutDegree
	 * @description 单个节点的出度
	 * @param Network net
	 * @param nodeId
	 * @return int outDegree
	 */
	public int nodeOutDegree(Network net, Number nodeId){
		return net.getOutDegreeNodesId(nodeId).size();
	}
	
	/**
	 * @name nodesOutDegree
	 * @description 多个节点的出度
	 * @param nodeIdSet
	 * @return HashMap<Number,Number> outDegrees
	 */
	public HashMap<Number,Number> nodesOutDegree(HashSet<Number> nodeIdSet){
		HashMap<Number,Number> degreeMap = new HashMap<Number,Number>();
		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			Number nodeId = (Number) iterator.next();
			degreeMap.put(nodeId, this.nodeOutDegree(nodeId));
		}
		return degreeMap;
	} 
	
	/**
	 * @name nodesOutDegree
	 * @description 多个节点的出度
	 * @param Network net
	 * @param nodeIdSet
	 * @return HashMap<Number,Number> outDegrees
	 */
	public HashMap<Number,Number> nodesOutDegree(Network net, HashSet<Number> nodeIdSet){
		HashMap<Number,Number> degreeMap = new HashMap<Number,Number>();
		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			Number nodeId = (Number) iterator.next();
			degreeMap.put(nodeId, this.nodeOutDegree(net, nodeId));
		}
		return degreeMap;
	}
	/**
	 * @name nodesDegreeMax
	 * @description 节点集合中度的最大值
	 * @return
	 */
	public int nodesDegreeMax(HashMap<Number, Number> degreeMap){
		int max = 0;
		max = MathTool.max(degreeMap.values());
		return max;
	}
	/**
	 * @name netDegreeMax
	 * @description 获取网络net的度最大值
	 * @return
	 */
	public int netDegreeMax(){
		Set<Number> nodeIdSet  = (Set<Number>) this.net.topology.keySet();
		int max = 0;
		int temp = 0;
		
 		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			temp = this.nodeDegree((Number) iterator.next());
			if(temp > max){
				max = temp;
			}
		}
 		return max;
	}
	
	/**
	 * @name netDegreeMax
	 * @description 获取网络net的度最大值
	 * @param net
	 * @return
	 */
	public int netDegreeMax(Network net){
		Set<Number> nodeIdSet  = (Set<Number>) net.topology.keySet();
		int max = 0;
		int temp = 0;
		
 		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			temp = this.nodeDegree(net, (Number) iterator.next());
			if(temp > max){
				max = temp;
			}
		}
 		return max;		
	}
	
	/**
	 * @name netDegreeDistribution
	 * @description 网络的度分布
	 * @return HashMap<Integer,Integer> degreeDistribution
	 */
	public HashMap<Number, Number> netDegreeDistribution(){
		Set<Number> nodeIdSet  = (Set<Number>) this.net.topology.keySet();
		int[] degreeArr = new int[this.netDegreeMax()+1];
 		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			degreeArr[this.nodeDegree((Number)iterator.next())]++;
		}
 		
 		HashMap<Number, Number> degreeMap = new HashMap<Number, Number>();
 		for (int i = 0; i < degreeArr.length; i++) {
			if(degreeArr[i] > 0){
				degreeMap.put(i, degreeArr[i]);
			}
		}
		return degreeMap;
	}
	
	/**
	 * @name netDegreeDistribution
	 * @description 网络的度分布
	 * @param net
	 * @return HashMap<Integer,Integer> degreeDistribution
	 */
	public HashMap<Number, Number> netDegreeDistribution(Network net){
		Set<Number> nodeIdSet  = (Set<Number>) net.topology.keySet();
		int[] degreeArr = new int[this.netDegreeMax(net)+1];
 		for (Iterator<Number> iterator = nodeIdSet.iterator(); iterator.hasNext();) {
			degreeArr[this.nodeDegree(net, (Number)iterator.next())]++;
		}
 		
 		HashMap<Number, Number> degreeMap = new HashMap<Number, Number>();
 		for (int i = 0; i < degreeArr.length; i++) {
			if(degreeArr[i] > 0){
				degreeMap.put(i, degreeArr[i]);
			}
		}
		return degreeMap;
	}
	
	/**
	 * @name netDegreeDistributionRate
	 * @description 网络的度分布率
	 * @return HashMap<Integer,Float> degreeDistribution
	 */
	public HashMap<Number, Number> netDegreeDistributionRate(){
		HashMap<Number, Number> degreeMap = this.netDegreeDistribution();
		return MathTool.rate(degreeMap);
	}
	
	/**
	 * @name netDegreeDistributionRate
	 * @description 网络的度分布率
	 * @param net
	 * @return HashMap<Integer,Float> degreeDistribution
	 */
	public HashMap<Number, Number> netDegreeDistributionRate(Network net){
		HashMap<Number, Number> degreeMap = this.netDegreeDistribution(net);
		return MathTool.rate(degreeMap);
	}
	
	/**
	 * @name netDegreeAvg
	 * @description 网络度的平均值
	 * @return
	 */
	public float netDegreeAvg(){
		HashMap<Number, Number> degreeMap = this.netDegreeDistribution();
		return MathTool.average(degreeMap);
	}
	
	/**
	 * @name netDegreeAvg
	 * @description 网络度的平均值
	 * @param net
	 * @return
	 */
	public float netDegreeAvg(Network net){
		HashMap<Number, Number> degreeMap = this.netDegreeDistribution(net);
		return MathTool.average(degreeMap);
	}
}
